Home:ALL Converter>Filter and Group in XSLT

Filter and Group in XSLT

Ask Time:2015-03-31T03:09:41         Author:Kapil

Json Formatter

I am trying to filter data and then group using XSLT. Here is my XML

    <?xml version="1.0" encoding="UTF-8"?>
<AccumulatedOutput>
    <root>
        <Header>
            <Add>true</Add>
            <Name>Subscriber</Name>
            <Value>SAC</Value>
        </Header>
    </root>
    <root>
        <Header>
            <Add>true</Add>
            <Name>System</Name>
            <Value>CBP</Value>
        </Header>
    </root>
    <root>
        <Header>
            <Add>false</Add>
            <Name>Subscriber</Name>
            <Value>SAC</Value>
        </Header>
    </root>
</AccumulatedOutput>

What I want to do is that group based on Header/Name and but remove the group in which Header/Add is false. So in above example I there will be two groups created (one for Name=Subscriber and other for Name=System) but since the first group(with Name=Subscriber) contains Add=false , I want to ignore that and my output should only have one node in it , like below

<?xml version = "1.0" encoding = "UTF-8"?>
<root>
    <Header>
        <Name>System</Name>
        <Value>CBP</Value>
        <Add>true</Add>
    </Header>
</root> 

I tried using group by method but I can't figure out a way to filter it. It will be a great help if someone can give me some pointers

Thanks

Author:Kapil,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29354280/filter-and-group-in-xslt
michael.hor257k :

In XSLT 2.0, you could do:\n\n<xsl:template match=\"/AccumulatedOutput\">\n <root>\n <xsl:for-each-group select=\"root/Header\" group-by=\"Name\">\n <xsl:if test=\"not(current-group()/Add='false')\">\n <xsl:copy-of select=\"current-group()\"/>\n </xsl:if>\n </xsl:for-each-group>\n </root>\n</xsl:template>\n",
2015-03-30T20:05:05
yy